package com.acoffee.java1;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 需求2
 *
 * @author acoffee
 * @create 2021-11-09 17:21
 */
public class BeanFactory2 {
    static Map<String, Person> map = new HashMap<>();

    static {
        Properties pros = new Properties();
        InputStream rs = BeanFactory2.class.getResourceAsStream("ApplicationContext.properties");
        String personName = "";
        try {
            pros.load(rs);
            Set<Object> keys = pros.keySet();
            for (Object key : keys) {
                personName = pros.getProperty(key + "");
                map.put(key + "", (Person) Class.forName(personName).newInstance());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Object getBean(String name) {
        Person person = map.get(name);
        return person;
    }

    @Test
    public void test1() {
        Person chinese = (Person) this.getBean("chinese");
        Person chinese1 = (Person) this.getBean("chinese");
        System.out.println(chinese);
        System.out.println(chinese1);
    }
}
